package com.unlcn.ils.erpInterfaces.service.buss.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.unlcn.ils.erpInterfaces.dao.mapper.SoshiplineMapper;
import com.unlcn.ils.erpInterfaces.dao.model.*;
import com.unlcn.ils.erpInterfaces.dao.model.bo.TrajectoryInfoBo;
import com.unlcn.ils.erpInterfaces.service.buss.*;
import com.unlcn.ils.erpInterfaces.service.util.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by lenovo on 2018/6/13.
 */
@Service
public class SoshiplineServiceImpl extends ServiceImpl<SoshiplineMapper, Soshipline> implements ISoshiplineService {

    @Autowired
    private ISmvehicleService smvehicleService;

    @Autowired
    private ICadaylocationService cadaylocationService;

    @Autowired
    private IGpsSmDriverLineService gpsSmDriverLineService;

    @Autowired
    private ISoshipheadService soshipheadService;

    @Autowired
    private ITmshipstatusCompassService iTmshipstatusCompassService;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void shipToByLicense(TrajectoryInfoBo trajectoryInfoBo) {

        // 检查数据
        checkData(trajectoryInfoBo);

        // 更新指令明细的状态
       String vcshipno = trajectoryInfoBo.getVcshipno();
       if(StringUtils.isEmpty(vcshipno)){
           throw new BaseException("调度指令号为空");
       }
       Soshiphead soshiphead = soshipheadService.getSoshipheadByShipNo(vcshipno);
       if(Objects.isNull(soshiphead)){
           throw new BaseException("找不到存在的调度指令");
       }

       Long ishipId = soshiphead.getIlineid();
       TmshipstatusCompass tmshipstatusCompass = iTmshipstatusCompassService.gettmsShipstatus(ishipId,15L,2);
       if(Objects.isNull(tmshipstatusCompass)){
           tmshipstatusCompass = new TmshipstatusCompass();
           tmshipstatusCompass.setIlineid(iTmshipstatusCompassService.getNextIlineid());
           tmshipstatusCompass.setDtstat(new Date());
           tmshipstatusCompass.setIshipid(ishipId);
           tmshipstatusCompass.setIstat(15L);
           tmshipstatusCompass.setIsource(2);
           tmshipstatusCompass.setVclatitude(trajectoryInfoBo.getDcy().toString());
           tmshipstatusCompass.setVclongitude(trajectoryInfoBo.getDcx().toString());
           tmshipstatusCompass.setVcmobile(trajectoryInfoBo.getMobile());
           tmshipstatusCompass.setVcshipno(vcshipno);
           tmshipstatusCompass.setDtdate(new Date());
           iTmshipstatusCompassService.insert(tmshipstatusCompass);
       }

        // 更新轨迹
        addTrajectory(trajectoryInfoBo);
    }

    @Transactional(propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void addOnWayByLicense(TrajectoryInfoBo trajectoryInfoBo) {

        // 检查数据
        checkData(trajectoryInfoBo);

        // 添加轨迹
        addTrajectory(trajectoryInfoBo);
    }

    @Transactional(propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void addTrajectory(TrajectoryInfoBo trajectoryInfoBo) {

        // 获取订单车辆信息
        Wrapper<Smvehicle> smvehicleWrapper = new EntityWrapper();
        smvehicleWrapper.eq("VCLICENSE", trajectoryInfoBo.getLicense());
        List<Smvehicle> smvehicleList = smvehicleService.selectList(smvehicleWrapper);
        Smvehicle smvehicle = null;
        if (CollectionUtils.isEmpty(smvehicleList)) {
            throw new BaseException("无法找到定位的车辆信息（"+ trajectoryInfoBo.getLicense() +"）");
        }
        smvehicle = smvehicleList.get(0);

        Cadaylocation cadaylocation = new Cadaylocation();
        // 车辆 id
        cadaylocation.setIvehicleid((double)smvehicle.getIlineid());
        // 当天日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String vcday = simpleDateFormat.format(new Date());
        cadaylocation.setVcday(Double.valueOf(vcday));
        // 省份
        cadaylocation.setVcprovince(trajectoryInfoBo.getProvince());
        // 城市
        cadaylocation.setVccityname(trajectoryInfoBo.getCity());
        // 最后更新时间
        cadaylocation.setDtlocation(new Date());
        // 创建时间
        cadaylocation.setDtdate(new Date());

        // 查询当天有没有存在此车辆的位置信息
        Wrapper<Cadaylocation> cadaylocationWrapper = new EntityWrapper<>();
        cadaylocationWrapper.eq("VCDAY", vcday);
        cadaylocationWrapper.eq("IVEHICLEID", cadaylocation.getIvehicleid());
        List<Cadaylocation> cadaylocationList = cadaylocationService.selectList(cadaylocationWrapper);
        if (CollectionUtils.isEmpty(cadaylocationList)) {
            // 不存在，插入车辆在每日位置
            cadaylocation.setIlineid(cadaylocationService.getNextIlineid());
            cadaylocationService.insert(cadaylocation);
        } else {
            // 存在，更新车辆在每日位置
            cadaylocation.setIlineid(cadaylocationList.get(0).getIlineid());
            cadaylocationService.updateById(cadaylocation);
        }


        GpsSmDriverLine gpsSmDriverLine = new GpsSmDriverLine();
        // 司机电话
        gpsSmDriverLine.setVcmobile(trajectoryInfoBo.getMobile());
        // 经度
        gpsSmDriverLine.setDcx(trajectoryInfoBo.getDcx());
        // 纬度
        gpsSmDriverLine.setDcy(trajectoryInfoBo.getDcy());
        // 创建时间
        gpsSmDriverLine.setDtdate(new Date());
        // 车辆 id
        gpsSmDriverLine.setIvehicleid(smvehicle.getIlineid());
        // 省份
        gpsSmDriverLine.setVcprovince(trajectoryInfoBo.getProvince());
        // 城市名称
        gpsSmDriverLine.setVccityname(trajectoryInfoBo.getCity());
        // 详细地址
        gpsSmDriverLine.setVclocation(trajectoryInfoBo.getLocation());
        // 操作人
        gpsSmDriverLine.setVcuserno(trajectoryInfoBo.getDriverName());
        // 主键 id
        gpsSmDriverLine.setIlineid(gpsSmDriverLineService.getNextIlineid());
        // 更新轨迹
        gpsSmDriverLineService.insert(gpsSmDriverLine);

        Smvehicle updateVehicle = new Smvehicle();
        // 车辆 id
        updateVehicle.setIlineid(smvehicle.getIlineid());
        // 经度
        updateVehicle.setDcx(trajectoryInfoBo.getDcx());
        // 纬度
        updateVehicle.setDcy(trajectoryInfoBo.getDcy());
        // 省份
        updateVehicle.setVcprovince(trajectoryInfoBo.getProvince());
        // 城市
        updateVehicle.setVccityname(trajectoryInfoBo.getCity());
        // 详细地址
        updateVehicle.setVclocation(trajectoryInfoBo.getLocation());
        // 最后一次跟踪的时间
        updateVehicle.setDtlocationdate(new Date());
        // 操作人
        updateVehicle.setVcuserno(trajectoryInfoBo.getDriverName());
        // 更新车辆最新位置
        smvehicleService.updateById(updateVehicle);
    }

    @Override
    public void checkData(TrajectoryInfoBo trajectoryInfoBo) {

        if (StringUtils.isEmpty(trajectoryInfoBo.getDriverName())) {
            throw new BaseException("司机不能为空");
        }

        if (StringUtils.isEmpty(trajectoryInfoBo.getMobile())) {
            throw new BaseException("司机电话不能为空");
        }

        if (StringUtils.isEmpty(trajectoryInfoBo.getLicense())) {
            throw new BaseException("车牌号不能为空");
        }

        if (trajectoryInfoBo.getDcx() == null) {
            throw new BaseException("经度不能为空");
        }

        if (trajectoryInfoBo.getDcy() == null) {
            throw new BaseException("玮度不能为空");
        }

        if (StringUtils.isEmpty(trajectoryInfoBo.getProvince())) {
            throw new BaseException("省份不能为空");
        }

        if (StringUtils.isEmpty(trajectoryInfoBo.getCity())) {
            throw new BaseException("城市不能为空");
        }
    }

    @Override
    public Date getNewestShipComedate(Long shipId) {
        return baseMapper.getNewestComedate(shipId);
    }
}
